package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list", name = "进入用户管理页面")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.findPage(getCompanyId(), page, pageSize);
        request.setAttribute("page", pageInfo);
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toAdd", name = "进入新增用户页面")
    public String toAdd() {
        //查询所有本企业部门数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList", deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit", name = "保存用户")
    public String edit(User user) {
        //TODO 将用户密码保存到session中
        //得到用户id和密码
        String password = user.getPassword();
        request.getSession().setAttribute("password",password);

        //id为空是新增,反之是修改
        if (StringUtils.isEmpty(user.getId())) {
            //新增时需设置
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            user.setCreateTime(new Date());

            //设置一个唯一的id
            user.setId(UUID.randomUUID().toString());
            //保存用户
            userService.save(user);
        } else {
            //修改用户
            user.setUpdateTime(new Date());

            userService.update(user);
        }
        //重定向到列表数据
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "进入修改保存页面")
    public String toUpdate(String id) {
        //根据id查询用户
        User user = userService.findById(id);
        //把user放入request域中
        request.setAttribute("user", user);

        //查询所有本企业部门数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList", deptList);
        //修改页面和新增页面公用一套
        return "system/user/user-add";
    }

    @RequestMapping(value = "/delete", name = "删除用户")
    public String delete(String id) {
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/roleList", name = "进入给用户分配角色页面")
    public String roleList(String id) {
        //1.查询本企业的所有角色
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList", roleList);

        //2.当前用户对象
        User user = userService.findById(id);
        request.setAttribute("user", user);

        //3.当前用户拥有的角色ids字符串
        List<String> roleIdList = userService.findRoleIdsByUserId(id);
        //将角色id转成字符串,用,分隔  流式表达
        String userRoleStr = roleIdList.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr", userRoleStr);

        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole", name = "保存用户角色中间表数据")
    public String changRole(String userId, String[] roleIds) {
        userService.changeRole(userId, roleIds);
        return "redirect:/system/user/list.do";
    }

}
